<?php

require_once dirname(__FILE__).'/../lib/moduloGeneratorConfiguration.class.php';
require_once dirname(__FILE__).'/../lib/moduloGeneratorHelper.class.php';

/**
 * modulo actions.
 *
 * @package    sistema_estadisticas
 * @subpackage modulo
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class moduloActions extends autoModuloActions
{

	public function execute($request)
	{
		if (!$request->isXmlHttpRequest())
		{
			$malla_id = $request->getUrlParameter('malla_id');
	
			if (!MallaCurricularTable::getInstance()->hasAccess($malla_id, $this->getUser()->getUserId()))
				$this->forward('login', 'secure');
	
			$this->getContext()->getRouting()->setDefaultParameter('malla_id', $malla_id);
	
			if ($id = $request->getUrlParameter('id'))
				$this->getContext()->getRouting()->setDefaultParameter('id', $id);
		}

		return parent::execute($request);
	}

	public function executeBuscar(sfWebRequest $request)
	{
		$carrera = $request->getParameter('carrera');
		$json_data = array();

		$resultado = ModuloTable::getInstance()->findByCarreraId($carrera);

		foreach ($resultado as $modulo)
			$json_data[] = array('id' => $modulo->id, 'nombre' => $modulo->__toString());

		return $this->renderText(json_encode($json_data));
	}

	public function executeNew(sfWebRequest $request)
	{
		$this->modulo = new Modulo();
		$this->modulo->malla_id = $request->getParameter('malla_id');
		$this->form = $this->configuration->getForm($this->modulo);
	}

	public function executeCreate(sfWebRequest $request)
	{
		parent::executeCreate($request);
		$this->form->getObject()->setMallaId($request->getParameter('malla_id'));
	}

	protected function buildQuery()
	{
		$query = parent::buildQuery();
		$query->addWhere('malla_id = ?', $this->getRequest()->getUrlParameter('malla_id'));

		return $query;
	}

}
